package org.zwy.gradetrack.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.zwy.gradetrack.entity.InternshipUnit;
import org.zwy.gradetrack.mapper.InternshipUnitMapper;
import org.zwy.gradetrack.service.InternshipUnitService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 实习单位服务实现类
 */
@Service
public class InternshipUnitServiceImpl extends ServiceImpl<InternshipUnitMapper, InternshipUnit> implements InternshipUnitService {

    @Override
    public Page<InternshipUnit> pageInternshipUnits(Page<InternshipUnit> page, String name) {
        LambdaQueryWrapper<InternshipUnit> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(name)) {
            wrapper.like(InternshipUnit::getName, name);
        }
        wrapper.orderByDesc(InternshipUnit::getCreateTime);
        return page(page, wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createInternshipUnit(InternshipUnit internshipUnit) {
        // 检查单位名称是否已存在
        LambdaQueryWrapper<InternshipUnit> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(InternshipUnit::getName, internshipUnit.getName());
        if (count(wrapper) > 0) {
            throw new RuntimeException("单位名称已存在");
        }
        
        // 设置创建和更新时间
        LocalDateTime now = LocalDateTime.now();
        internshipUnit.setCreateTime(now);
        internshipUnit.setUpdateTime(now);
        
        return save(internshipUnit);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateInternshipUnit(InternshipUnit internshipUnit) {
        // 检查单位是否存在
        InternshipUnit existingUnit = getById(internshipUnit.getId());
        if (existingUnit == null) {
            throw new RuntimeException("实习单位不存在");
        }
        
        // 检查单位名称是否与其他单位重复
        if (!existingUnit.getName().equals(internshipUnit.getName())) {
            LambdaQueryWrapper<InternshipUnit> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(InternshipUnit::getName, internshipUnit.getName());
            if (count(wrapper) > 0) {
                throw new RuntimeException("单位名称已存在");
            }
        }
        
        // 设置更新时间
        internshipUnit.setUpdateTime(LocalDateTime.now());
        
        return updateById(internshipUnit);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteInternshipUnit(Long id) {
        // 检查单位是否存在
        if (getById(id) == null) {
            throw new RuntimeException("实习单位不存在");
        }
        return removeById(id);
    }

    @Override
    public InternshipUnit getInternshipUnitById(Long id) {
        InternshipUnit internshipUnit = getById(id);
        if (internshipUnit == null) {
            throw new RuntimeException("实习单位不存在");
        }
        return internshipUnit;
    }

    @Override
    public List<InternshipUnit> getInternshipUnitsByName(String name) {
        LambdaQueryWrapper<InternshipUnit> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(InternshipUnit::getName, name);
        wrapper.orderByDesc(InternshipUnit::getCreateTime);
        return list(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchImport(List<InternshipUnit> internshipUnits) {
        if (internshipUnits == null || internshipUnits.isEmpty()) {
            throw new RuntimeException("导入数据不能为空");
        }
        
        // 检查单位名称是否重复
        for (InternshipUnit unit : internshipUnits) {
            LambdaQueryWrapper<InternshipUnit> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(InternshipUnit::getName, unit.getName());
            if (count(wrapper) > 0) {
                throw new RuntimeException("单位名称已存在：" + unit.getName());
            }
        }
        
        // 设置创建和更新时间
        LocalDateTime now = LocalDateTime.now();
        for (InternshipUnit unit : internshipUnits) {
            unit.setCreateTime(now);
            unit.setUpdateTime(now);
        }
        
        return saveBatch(internshipUnits);
    }
} 